/**
 * VYPe11 Project
 * VYPe11 language compiler.
 * Authors: Jozef Lang (xlangj01), 
 * 			Zoltan Zemko (xzemko01)
 * 2011
 */
package cz.vutbr.fit.VYPe.team18.VYPe11.nodes.expression.bracket;

import java.io.IOException;

import cz.vutbr.fit.VYPe.team18.VYPe11.exception.SematicException;
import cz.vutbr.fit.VYPe.team18.VYPe11.nodes.IdentifierNode;
import cz.vutbr.fit.VYPe.team18.VYPe11.nodes.data_type.DataType;
import cz.vutbr.fit.VYPe.team18.VYPe11.nodes.expression.ExpressionNode;

public class IndexingNode extends ExpressionNode {
	private IdentifierNode id;
	private ExpressionNode expression;
	
	public IndexingNode(IdentifierNode id, 
							ExpressionNode expression) {
		this.id=id;
		this.expression=expression;
	}

	public IdentifierNode getId() {
		return id;
	}

	public ExpressionNode getExpression() {
		return expression;
	}
	
	public DataType getType() throws SematicException {
		if(expressionType!=null)
			return expressionType;
		
		DataType dt=expression.getType();
		
		if(dt==DataType.SHORT || dt==DataType.INT)
			expressionType=dt;
		else
			throw new SematicException("Index must be numerical value.");
		return expressionType;
	}
	
	@Override
	public void generateTAC() throws IOException {
		expression.generateTAC();
		int i=tacCounter.getTermIndex()-1;
		
		writeTAC("index",this.id.getId(),"_t"+i,"_t"+tacCounter.getNextTermIndex());
	}
}
